package com.jiukuaidao.zhulin.module.address.presenter;

import com.jiukuaidao.library.net.MyRetroCallBack;
import com.jiukuaidao.zhulin.AppContext;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.api.ApiGetAddAddress;
import com.jiukuaidao.zhulin.data.TasksRepository;
import com.jiukuaidao.zhulin.entity.ApiHistoryAddressInfo;
import com.jiukuaidao.zhulin.module.BasePresenter;
import com.jiukuaidao.zhulin.module.address.contract.AddAddressContract;
import com.ta.utdid2.android.utils.StringUtils;

/**
 * Author : PaiTaSuo
 * Date : 2016/6/6
 * Description :
 */
public class AddAddressPresenter extends BasePresenter<AddAddressContract.View, TasksRepository> implements AddAddressContract.Presenter {
    public AddAddressPresenter(TasksRepository repository, AddAddressContract.View view) {
        super(repository, view);
    }

    @Override
    public void start() {

        loadData();
    }

    @Override
    public void loadData() {
        if (StringUtils.isEmpty(mView.getAddressInfo().address)) {
            mView.toast(AppContext.getContext().getString(R.string.addressinfo_notnull));
            return;
        } else if (StringUtils.isEmpty(mView.getAddressInfo().street)) {
            mView.toast(AppContext.getContext().getString(R.string.housenum_notnull));
            return;
        } else if (StringUtils.isEmpty(mView.getAddressInfo().mobile)) {
            mView.toast(AppContext.getContext().getString(R.string.phone_null));
            return;
        } else if (mView.getAddressInfo().mobile.length() != 11) {
            mView.toast(AppContext.getContext().getString(R.string.phone_error));
            return;
        } else if (StringUtils.isEmpty(mView.getAddressInfo().consignee)){
            mView.toast(AppContext.getContext().getString(R.string.name_notnull));
            return;
        }
        mRepository.loadData(new ApiGetAddAddress(mView.getAddressInfo(), new MyRetroCallBack<ApiHistoryAddressInfo.AddressInfo>() {
            @Override
            public void onSuccess(ApiHistoryAddressInfo.AddressInfo result, String errorCode, String msg) {
                mView.intentJump(result.shopId);
                mView.toast(msg);
            }

            @Override
            public void onFail(ApiHistoryAddressInfo.AddressInfo result, String errorCode, String msg) {

            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {

            }
        }
        ));
    }
}
